//
//  OrderViewController.swift
//  EasyDana
//
//  Created by mac on 2024/10/17.
//

import UIKit
import RxSwift
import RxCocoa
class OrderViewController: UITableViewController {

    private let bag = DisposeBag()
    
    var status: Int = -1
    
    private var source: [OrderModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
    }
    
    private func configView() {
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = UIColor.hex(with: "#f7f7f7")
        self.tableView.register(UINib(nibName: "OrderViewCell", bundle: Bundle.main), forCellReuseIdentifier: "OrderViewCell")
        self.tableView.showsVerticalScrollIndicator = false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.getData()
    }
    
    private func getData() {
        Task {
            struct OrdersDataModel: Codable {
                let tilly: [OrderModel]?
                init(from decoder: any Decoder) throws {
                    let container = try decoder.container(keyedBy: CodingKeys.self)
                    self.tilly = try container.decodeIfPresent([OrderModel].self, forKey: .tilly)
                }
            }
            do {
                self.showLoading(text: nil)
                let res = try await HTTPManager.default.post(path: Api.getOrders, param: ["decency": self.status], dataType: OrdersDataModel.self)
                self.source = res.data?.tilly ?? []
                self.tableView.reloadData()
                if self.source.count == 0 {
                    let bgView = UIView()
                    bgView.backgroundColor = UIColor.white
                    self.tableView.backgroundView = bgView
                    let emptyImageView = UIImageView(image: UIImage(named: "order_empty"))
                    bgView.addSubview(emptyImageView)
                    emptyImageView.snp.makeConstraints { make in
                        make.top.equalToSuperview().offset(80)
                        make.centerX.equalToSuperview()
                    }
                } else {
                    self.tableView.backgroundView = nil
                }
                self.hideLoading()
            } catch {
                print(error)
                self.hideLoading()
            }
        }
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.source.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OrderViewCell") as! OrderViewCell
        cell.model = self.source[indexPath.section]
        cell.agreementAction = { url in
            UIViewController.top
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.source[indexPath.section]
        if let url = URL(string: model.kirk) {
            self.jump(url: url)
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 232
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.hex(with: "#F7F7F7")
        return view
    }

}
